package top.lanscarlos.ashcraft.controller;

import com.google.gson.JsonObject;
import com.sun.istack.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import top.lanscarlos.ashcraft.pojo.Commodity;
import top.lanscarlos.ashcraft.service.CommodityService;

import java.util.List;

/**
 * Ashcraft
 * top.lanscarlos.ashcraft.controller
 *
 * @author Lanscarlos
 * @since 2022-05-26 09:54
 */

@RestController
@RequestMapping(value = "/commodity")
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class CommodityController extends AshController {
    @Autowired
    private CommodityService service;

    @RequestMapping("/query")
    public Object query(String keyword) {
        List<Commodity> commodities = service.query(keyword, 0);
//        if (sellers.isEmpty()) {
//            json.addProperty("result", false);
//        } else {
//            json.addProperty("result", true);
//        }
//        json.add("items", toJson(sellers));
        return toJson(commodities);
    }

//    @RequestMapping("/query")
//    public Object query(String keyword, int sellerId) {
//        List<Commodity> commodities = service.query(keyword, sellerId);
////        if (sellers.isEmpty()) {
////            json.addProperty("result", false);
////        } else {
////            json.addProperty("result", true);
////        }
////        json.add("items", toJson(sellers));
//        return toJson(commodities);
//    }

    @RequestMapping("/create")
    public Object create(
            int sellerId,
            @NotNull String name,
            @NotNull String description,
            @NotNull String image,
            @NotNull double price,
            @NotNull int storage,
            @NotNull String category
    ) {
        Commodity commodity = service.create(sellerId, name, description, image, price, storage, category);
        return toJson(commodity);
    }
}
